package com.neuedu.elmserver.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.neuedu.elmserver.mapper.ActivityMapper;
import com.neuedu.elmserver.pojo.Activity;
import com.neuedu.elmserver.pojo.Business;
import com.neuedu.elmserver.mapper.BusinessMapper;
import com.neuedu.elmserver.service.BusinessService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.neuedu.elmserver.utils.cmpDistance;
import com.neuedu.elmserver.utils.cmpRemarks;
import com.neuedu.elmserver.utils.cmpSales;
import com.neuedu.elmserver.vo.ResultVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Iterator;
import java.util.List;

/**
 * <p>
 * 商家表 服务实现类
 * </p>
 *
 * @author wyh
 * @since 2024-11-07
 */
@Service
public class BusinessServiceImpl extends ServiceImpl<BusinessMapper, Business> implements BusinessService {

    @Autowired
    private BusinessMapper businessMapper;

    @Autowired
    private ActivityMapper activityMapper;

    /**
     * 根据类型查询商家列表
     * @param typeId
     * @return
     */
    @Override
    public ResultVO queryBusinessByType(Integer typeId) {
        QueryWrapper qw=new QueryWrapper();
        qw.eq("typeId",typeId);
        return ResultVO.ok("查询成功",businessMapper.selectList(qw));
    }

    /**
     * 根据编号查询商家信息
     * @param id
     * @return
     */
    @Override
    public ResultVO queryBusinessById(Integer id) {
        return ResultVO.ok("查询成功", businessMapper.selectById(id));
    }

    /**
     * 根据综合评分查询商家 （大到小）
     * @return
     */
    @Override
    public ResultVO queryBusinessByRemarks() {
        List<Business> businessList = businessMapper.selectList(null);
        businessList.sort(new cmpRemarks());

        for(Business business:businessList){
            QueryWrapper qw=new QueryWrapper();
            qw.eq("business_id",business.getBusinessId());
            //System.out.println(business.getBusinessId());
            List<Activity> activities= activityMapper.selectList(qw);
            //System.out.println(activities);
            business.setActivities(activities);
        }
        return ResultVO.ok("查询成功", businessList);
    }

    /**
     * 根据距离查询商家 （小到大）
     * @return
     */
    @Override
    public ResultVO queryBusinessByDistance() {
        List<Business> businessList = businessMapper.selectList(null);
        businessList.sort(new cmpDistance());

        for(Business business:businessList){
            QueryWrapper qw=new QueryWrapper();
            qw.eq("business_id",business.getBusinessId());
            //System.out.println(business.getBusinessId());
            List<Activity> activities= activityMapper.selectList(qw);
            //System.out.println(activities);
            business.setActivities(activities);
        }
        return ResultVO.ok("查询成功", businessList);
    }

    /**
     * 根据销量查询商家 （大到小）
     * @return
     */
    @Override
    public ResultVO queryBusinessBySales() {
        List<Business> businessList = businessMapper.selectList(null);
        businessList.sort(new cmpSales());

        for(Business business:businessList){
            QueryWrapper qw=new QueryWrapper();
            qw.eq("business_id",business.getBusinessId());
            //System.out.println(business.getBusinessId());
            List<Activity> activities= activityMapper.selectList(qw);
            //System.out.println(activities);
            business.setActivities(activities);
        }
        return ResultVO.ok("查询成功", businessList);
    }

    @Override
    public ResultVO getCategorisedBusiness(List<Business> businessList, Integer typeId) {
        QueryWrapper qw = new QueryWrapper();
        qw.eq("typeId", typeId);
        List<Business> typedBusinessList = businessMapper.selectList(qw);

        Iterator<Business> iterator = businessList.iterator();

        while (iterator.hasNext()) {
            Business business = iterator.next();
            boolean flag = false;

            // 检查当前 business 是否在 typedBusinessList 中
            for (Business typedBusiness : typedBusinessList) {
                if (business.getBusinessId().equals(typedBusiness.getBusinessId())) {
                    flag = true;
                    break; // 找到匹配的 businessId，跳出内层循环
                }
            }

            // 如果未找到匹配的 businessId，移除当前 business
            if (!flag) {
                iterator.remove(); // 使用 iterator.remove() 安全地移除元素
            }
        }

        return ResultVO.ok("查询成功", businessList);
    }

    /**
     * 查询所有商家的数量
     * @return
     */
    @Override
    public ResultVO getAllBusinessListCount() {
        QueryWrapper qw = new QueryWrapper();

        return ResultVO.ok("查询成功", businessMapper.selectCount(qw));
    }

    /**
     * 根据排行编号查询商家信息
     * @param
     * @return
     */
    @Override
    public ResultVO queryBusinessByRankIdAsc() {

        QueryWrapper<Business> qw = new QueryWrapper<>();
        qw.orderByAsc("rank_id");
        List<Business> businessEntities = businessMapper.selectList(qw);
        return ResultVO.ok("查询成功", businessEntities);

    }

}
